// 任务控制器
import taskService from '../services/taskService.js';

/**
 * 创建任务控制器
 * 处理创建新任务的请求
 * @param {Object} request - Fastify请求对象，包含任务信息
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 创建结果响应
 */
async function createTask(request, reply) {
  try {
    const { group_id, title, description, is_important, is_my_day, due_date } = request.body;
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const result = await taskService.createTask(userId, group_id, title, description, is_important, is_my_day, due_date);
    return reply.code(201).send({
      code: 1,
      success: true,
      message: '创建成功',
      data: result
    });
  } catch (error) {
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 获取任务列表控制器
 * 处理获取用户任务列表的请求
 * @param {Object} request - Fastify请求对象，包含查询参数
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 任务列表响应
 */
async function getTasks(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const { group_id, status } = request.query;
    const result = await taskService.getTasks(userId, group_id, status);
    return reply.send({
      code: 1,
      success: true,
      message: '获取成功',
      data: result
    });
  } catch (error) {
    return reply.code(500).send({
      code: 10006,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 获取"我的一天"任务控制器
 * 处理获取用户标记为"我的一天"的任务列表的请求
 * @param {Object} request - Fastify请求对象
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} "我的一天"任务列表响应
 */
async function getMyDayTasks(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const result = await taskService.getMyDayTasks(userId);
    return reply.send({
      code: 1,
      success: true,
      message: '获取成功',
      data: result
    });
  } catch (error) {
    return reply.code(500).send({
      code: 10006,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 获取任务详情控制器
 * 处理获取特定任务详情的请求
 * @param {Object} request - Fastify请求对象，包含任务ID参数
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 任务详情响应
 */
async function getTaskDetail(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const { taskId } = request.params;
    const result = await taskService.getTaskDetail(userId, parseInt(taskId));
    return reply.send({
      code: 1,
      success: true,
      message: '获取成功',
      data: result
    });
  } catch (error) {
    if (error.message === 'Task not found or access denied') {
      return reply.code(404).send({
        code: 10004,
        success: false,
        message: error.message,
        data: {}
      });
    }
    return reply.code(500).send({
      code: 10006,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 更新任务控制器
 * 处理更新任务信息的请求
 * @param {Object} request - Fastify请求对象，包含任务ID和更新信息
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 更新结果响应
 */
async function updateTask(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const { taskId } = request.params;
    const { group_id, title, description, is_important, is_my_day, status, due_date } = request.body;
    const result = await taskService.updateTask(userId, parseInt(taskId), group_id, title, description, is_important, is_my_day, status, due_date);
    return reply.send({
      code: 1,
      success: true,
      message: '更新成功',
      data: result
    });
  } catch (error) {
    if (error.message === 'Task not found or access denied') {
      return reply.code(404).send({
        code: 10004,
        success: false,
        message: error.message,
        data: {}
      });
    }
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 删除任务控制器
 * 处理删除任务的请求
 * @param {Object} request - Fastify请求对象，包含任务ID参数
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 删除结果响应
 */
async function deleteTask(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const { taskId } = request.params;
    await taskService.deleteTask(userId, parseInt(taskId));
    return reply.send({
      code: 1,
      success: true,
      message: '删除成功',
      data: {}
    });
  } catch (error) {
    if (error.message === 'Task not found or access denied') {
      return reply.code(404).send({
        code: 10004,
        success: false,
        message: error.message,
        data: {}
      });
    }
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

/**
 * 更新任务分组控制器
 * 处理更新任务所属分组的请求
 * @param {Object} request - Fastify请求对象，包含任务ID和新分组ID
 * @param {Object} reply - Fastify响应对象
 * @returns {Object} 更新结果响应
 */
async function updateTaskGroup(request, reply) {
  try {
    const userId = request.user.userId; // 从认证中间件获取用户ID
    const { taskId } = request.params;
    const { group_id } = request.body;
    const result = await taskService.updateTaskGroup(userId, parseInt(taskId), group_id);
    return reply.send({
      code: 1,
      success: true,
      message: '更新成功',
      data: result
    });
  } catch (error) {
    if (error.message === 'Task not found or access denied') {
      return reply.code(404).send({
        code: 10004,
        success: false,
        message: error.message,
        data: {}
      });
    }
    return reply.code(400).send({
      code: 10001,
      success: false,
      message: error.message,
      data: {}
    });
  }
}

export default { createTask, getTasks, getMyDayTasks, getTaskDetail, updateTask, deleteTask, updateTaskGroup };